from collections import deque
def max_diff(n,k,f):
    # 双端队列
    q=deque()
    ans=[]
    for i in range(n):
        if f:
            # 递增 q[0] 是最小值
            while q and a[q[-1]] >= a[i]:
                q.pop()
        else:
            # 递减 q[0] 是最大值
            while q and a[q[-1]] <= a[i]:
                q.pop()
        q.append(i)
        if q[0] <= i-k:
            q.popleft()
        ans.append(a[q[0]])
    return ans

n,k=map(int,input().split())
a=list(map(int,input().split()))
F=max_diff(n,k,True)
G=max_diff(n,k,False)
print(max((G[i]-F[i]) for i in range(n)))